import { defineStore } from 'pinia'

import { AppState, Dict, DictDTO } from './data.d'

import { getDictList } from './api'
import { generateNewList } from '@/utils/tool'

export const useAppStore = defineStore('appStore', {
  state: (): AppState => ({
    // 字典列表
    dictList: [],
  }),
  actions: {
    async getDictListAction() {
      const { data } = await getDictList()
      this.dictList = generateNewList<DictDTO, Dict>({
        menuList: data.records,
        target: 'dictType',
        mapper: (key, value) => {
          return {
            dictType: key,
            dictList: value,
          }
        },
      })
    },
  },
})
